package com.aft.terra.modules.game.controller;

import java.util.Date;
import java.util.Enumeration;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.aft.terra.app.Constants;
import com.aft.terra.common.bean.Message;
import com.aft.terra.common.interceptor.PhoneInterceptor;
import com.aft.terra.common.interceptor.StartEndDateInterceptor;
import com.aft.terra.modules.basic.BaseController;
import com.aft.terra.modules.basic.model.Adver;
import com.aft.terra.modules.basic.model.Config;
import com.aft.terra.modules.game.model.GameDetails;
import com.aft.terra.modules.game.model.User;
import com.aft.terra.modules.game.service.GameService;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.kit.LogKit;
import com.jfinal.plugin.activerecord.Record;

@Before(StartEndDateInterceptor.class)
public class GameController  extends BaseController<GameDetails> {
	
	/**
	 * 获取用户当前有多少的游戏机会
	 */
	@Clear(StartEndDateInterceptor.class)
	public void querUserGameCount(){
		String userId = getCookie(Constants.SESSION_NAME);   
		renderJson(GameService.service.querUser(userId));
	}
	
	/**
	 * 游戏开始
	 */
	@Clear
	public void startGame(){
		String userId = "11"; 
		renderJson(GameService.service.startGame(this,userId));
	}
	
	/**
	 * 获取游戏是否开启
	 */
	@Clear(StartEndDateInterceptor.class)
	public void queryGameStartDateOnEndDate(){
		renderJson(GameService.service.queryGameStartDateOnEndDate());
	}
	
	
	/**
	 * 获取总榜排行榜数据
	 */
	@Clear(StartEndDateInterceptor.class)
	public void queryRankingList(){
		renderJson(GameService.service.queryRankingList());
	}
	
	/**
	 * 获取日榜排行榜数据
	 */
	@Clear(StartEndDateInterceptor.class)
	public void queryOnList(){
		renderJson(GameService.service.queryOnList(getPara("date")));
	}
	
	/**
	 * 周榜排行榜数据
	 */
	@Clear(StartEndDateInterceptor.class)
	public void queryWeeklyChart(){
		renderJson(GameService.service.queryWeeklyChart(getPara("date")));
	}
	
	/**
	 * 月榜排行榜数据
	 */
	@Clear(StartEndDateInterceptor.class)
	public void queryMonthlyList(){
		renderJson(GameService.service.queryMonthlyList(getPara("date")));
	}
	
	/**
	 * 更新游戏数据
	 */
	public void updateGameData(){
		LogKit.info("这里真的好了i");
		String userId = getCookie(Constants.SESSION_NAME);   
		renderJson(GameService.service.updateGameData(getPara("gdId"),userId, getParaToInt("score",0)));
	}
	
	/**
	 * 保存手机号码
	 */
	@Clear(StartEndDateInterceptor.class)
	@Before({PhoneInterceptor.class,UserValidator.class})
	public void updateUserPhone(){
		String userId = getCookie(Constants.SESSION_NAME);
		User user = getAttr("user");
		Message message = GameService.service.updateUserPhone(getPara("phone"),userId,user);
		if(message.isSuccess()){
			setAttr("message", message.getMessage());
			render("/member/addPhone.html");
		}else{
			redirect("/main?userId="+userId);
		}
	}
	
	/**
	 * 随机抽奖
	 */
	public void randomLuckDraw(){
		String userId = getCookie(Constants.SESSION_NAME);   
		renderJson(GameService.service.randomLuckDraw(userId));
	}
	
	/**
	 * 分享添加游戏次数
	 */
	public void saveShareUserCount(){
		String userId = getCookie(Constants.SESSION_NAME); 
		renderJson(GameService.service.saveShareUserCount(userId,getPara(0)));
	}
	
	/**
	 * 获取用户支付笔数
	 */
	public void getUserOrders(){
		String userId = getCookie(Constants.SESSION_NAME);  
		renderJson(GameService.service.getUserOrders(userId));
	}
	
	/**
	 * 获取排行榜的名次
	 */
	public void getRankingIndex(){
		String userId = getCookie(Constants.SESSION_NAME);
		renderJson(GameService.service.getRankingIndex(userId,getPara("type"),getPara("date")));
	}
	
	/**
	 * 支付 1分钱
	 */
	@Clear
	public void getCcbPay(){
		String url = GameService.service.getCcbPay(this);
		redirect(url, true);
	}
	
	/**
	 * 新5毛钱支付
	 */
	public void getNewCcbPay(){
		String url = GameService.service.getNewCcbPay(this);
		redirect(url);
	}
	
	/**
	 * 两个支付自己掉自己
	 */
	@Clear
	public void getNewCallBackPay(){
		String url = GameService.service.getNewCallBackPay(this);
		redirect(url);
	}
	
	/**
	 * 只获取中的5元话费的奖品
	 */
	@Clear(StartEndDateInterceptor.class)
	public void getPrizeList(){
		renderJson(GameService.service.getPrizeList());
	}
	
	/**
	 * 获取rule 列表
	 */
	@Clear(StartEndDateInterceptor.class)
	public void getRuleList(){
		renderJson(GameService.service.getRuleList());
	}
	
	/**
	 * 获取往期列表
	 */
	public void getLuckDrawList(){
		renderJson(GameService.service.getLuckDrawList(getParaToInt(0)));
	}
	
	/**
	 * 获取广告
	 */
	public void getAdverList(){
		renderJson(GameService.service.getAdverList());
	}
	
	/**
	 * 跳转到添加手机的页面
	 */
	@Clear(StartEndDateInterceptor.class)
	public void addPhone(){
		render("/member/addPhone.html");
	}
	
	@Clear
	public void gameCache(){
		Config.dao.config();
		renderNull();
	}
	
	@Clear
	public void gameAdverCache(){
		Adver.dao.adver();
		renderNull();
	}
	
	
	/**
	 * 是否违反规则
	 */
	@Clear(StartEndDateInterceptor.class)
	public void getIntercept(){
		String userId = getCookie(Constants.SESSION_NAME);
		renderJson(GameService.service.getIntercept(userId));
	}
	
	@Clear
	public void getGameConfig(){
		renderJson(GameService.service.getNameConfig());
	}
	
	
	@Clear
	public void textParam(){
		 Map<String, String[]> map = getParaMap();
        JSONObject jsonObject = new JSONObject();
        for (Map.Entry<String, String[]> o : map.entrySet()) {
        	if(StringUtils.isNotBlank(StringUtils.join(o.getValue(), ","))){
        		jsonObject.put(o.getKey(), StringUtils.join(o.getValue(), ","));
        	}
        }
        renderJson(jsonObject.toJSONString());
	}
	
	
	@Clear
	public void testHeaderNames(){

	    Enumeration headerNames = getRequest().getHeaderNames();
	    JSONObject jsonObject = new JSONObject();
	    while (headerNames.hasMoreElements()) {
	        String key = (String) headerNames.nextElement();
	        String value = getRequest().getHeader(key);
	        jsonObject.put(key, value);
	    }
        renderJson(jsonObject.toJSONString());
	}
	
}
